package com.ybfenpan.controller.admin.wealth;


import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.service.SysPayService;
import com.ybfenpan.taglib.pagetag.Pagination;
import com.ybfenpan.utils.MapUtil;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2016/3/25.
 */
@Controller

public class SysPayListController extends BaseController {
    @Resource
    private SysPayService sysPayService;
   
   
    @RequestMapping("admin/wealth/sysPayList.shtml")
    public String list(HttpServletRequest request) throws Exception{
        HashMap formMap = getParameterMap(request);
        String startDate = MapUtil.getString(formMap, "startDate");
        String endDate = MapUtil.getString(formMap, "endDate");
        String loginNameDiy=MapUtil.getString(formMap, "loginNameDiy");
         
        if(startDate!=null&&!startDate.equals("")&&endDate!=null&&!endDate.equals("")){
        	//+1天
        	 
        	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟  
        	 
        	Date newEndDate=sdf.parse(endDate);  
        	
        	long temp=newEndDate.getTime()+24*60*60*1000;
			newEndDate=new Date(temp);
			
			String newEndDateString=sdf.format(newEndDate);  
        	formMap.put("where", " a.commitDate>= '"+startDate+"' and a.commitDate<= '"+newEndDateString+"'");	//  时间查询
        	
        	if(loginNameDiy!=null&&loginNameDiy!=""){
        		formMap.put("where", "  a.commitDate>= '"+startDate+"' and a.commitDate<= '"+newEndDateString+"' and b.loginName like '%"+loginNameDiy+"%'");			//未报单
            }
        	
        }else{
        	if(loginNameDiy!=null&&loginNameDiy!=""){
        		formMap.put("where", "1=1 and b.loginName like '%"+loginNameDiy+"%'");			//未报单
            }
        }
        
        
        
        
        formMap.put("pagination.orderBy"," a.commitDate desc");  //排列
        Pagination page = sysPayService.queryForPageList("sysPayForPage",formMap);
        request.setAttribute(Pagination.pageBeanName, page);
		request.setAttribute("startDate", startDate);
		request.setAttribute("endDate", endDate);
        return "admin/wealth/sysPayList";
    }

    
    @RequestMapping("admin/wealth/sysPayListOut.shtml")
    public void out(HttpServletRequest request, HttpServletResponse response) throws Exception{
        HashMap formMap = getParameterMap(request);
        String startDate = MapUtil.getString(formMap, "startDate");
        String endDate = MapUtil.getString(formMap, "endDate");
           
        if(startDate!=null&&!startDate.equals("")&&endDate!=null&&!endDate.equals("")){
        	//+1天
        	 
        	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟  
        	 
        	Date newEndDate=sdf.parse(endDate);  
        	
        	long temp=newEndDate.getTime()+24*60*60*1000;
			newEndDate=new Date(temp);
			
			String newEndDateString=sdf.format(newEndDate);  
        	formMap.put("where", " a.commitDate>= '"+startDate+"' and a.commitDate<= '"+newEndDateString+"'");	//  时间查询
        }
       
        
        formMap.put("memberLoginName",MapUtil.getString(formMap, "loginNameDiy"));  
        
        List list =sysPayService.queryForList("sysPayForList",formMap);
         
        System.out.println(list.size());
        
        String realPath = request.getRealPath("model/pay.xls");

		// 读取EXCEL文件
		FileInputStream fileIn=new FileInputStream(realPath);
		POIFSFileSystem fs=new POIFSFileSystem(fileIn);

		HSSFWorkbook wb=new HSSFWorkbook(fs);
		HSSFSheet sheet=wb.getSheetAt(0);
		
		
		//List集合里的是hashMap
        Map map = null;
        
        int num=1;
		
        for(int i=0;i<list.size();i++){
        	map=(HashMap)list.get(i);
        	String memberLoginName=(String)map.get("memberLoginName");
           
        	HSSFRow row=sheet.createRow(num);
			
			//代理商编号
			HSSFCell orderNo=row.createCell((short)0);
			orderNo.setCellValue(map.get("id").toString());
			
			//提现金额
			HSSFCell numM=row.createCell((short)1);
			numM.setCellValue(memberLoginName);
			
			//手续费
			HSSFCell fei=row.createCell((short)2);
			fei.setCellValue(map.get("commitDate").toString());
			 
			//状态
			HSSFCell state3=row.createCell((short)3);
			state3.setCellValue((Double)map.get("num"));
			
			//状态
			HSSFCell state4=row.createCell((short)4);
			state4.setCellValue((Double)map.get("num2"));
			
			
			num++;

        }
		 


		/***************************  文件下载  **************************************/
		
		//下载时显示的名称
		SimpleDateFormat formatter2 = new SimpleDateFormat("yyyyMMddHHmmss"); 
		String fileName = "充值记录("+formatter2.format(new Date())+")";
	
		// 设置文件下载
		
		response.setContentType("application/vnd.ms-excel");
		response.addHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes(), "iso8859-1") + ".xls");
	
		// 定义输出流
		ServletOutputStream out= response.getOutputStream();
		try {
			wb.write(out);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
			throw new Exception("文件输出出错！");
		} finally {
			fileIn.close();
			fileIn=null;
				
			out.close();
			out = null;
		} 
    }
    
}
